
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameingDemo {

    // 创建窗口对象
    Frame s = new Frame("TestWindows");
    //设置窗口高度宽度
    private final int W_Height=400 ;
    private final int W_Wdith=300 ;
    //球拍高宽
    private final int P_Height=20 ;
    private final int P_Wdith=60;
    //球拍坐标
    private int PX = 130;
    private final int PY = 340;

    //小球高宽
    private final int B_SIZE=16 ;
    //小球坐标
    private int BX = 120;
    private int BY = 20;
    //小球速度
    private int BXV = 10;
    private int BYV = 5;
    //判断游戏是否结束变量
    private boolean isover = false;

    //定义
    private Timer timer;

    //创建绘画组件
    public class MyCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
//绘制画面
//游戏结束内容
            if(isover = true){
                g.setColor(Color.RED);
                g.setFont(new Font("times",Font.BOLD,30));
                g.drawString("GAME OVER",20,200);
            }else{
                //游戏中内�?
                //绘制小球
                g.setColor(Color.RED);
                g.fillOval(BX,BY,B_SIZE,B_SIZE);
                //绘制球拍
                g.setColor(Color.PINK);
                g.fillRect(PX,PY,P_Wdith,P_Height);


            }
        }}

        MyCanvas myc = new MyCanvas();

        public void mix(){
            //球拍坐标处理逻辑
            KeyListener k = new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    //获取当前按下按钮
                    int kc= e.getKeyCode();
                    if (kc == KeyEvent.VK_LEFT){
                        if (PX>0){
                            PX-=10;
                        }
                    }else if (kc == KeyEvent.VK_RIGHT){
                        if (PX<(W_Wdith-P_Wdith)){
                            PX+=10;
                        }
                    }
                }
            };
            //给frame 和画布注册监�?
            s.addKeyListener(k);
            myc.addKeyListener(k);
//小球处理逻辑
            ActionListener task = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //修正速度
                    if (BX<=0||BX>(W_Wdith-B_SIZE)){
                        BXV=-BXV;
                    }
                    if (BY<=0||(BY>P_Height-B_SIZE && BX>PX && BX<PX+P_Wdith)){
                        BYV=-BYV;
                    }
                    //游戏状�?�修�?
                    if ((BX<PX||BX>PX+P_Wdith) && BY > PY-B_SIZE){
                        timer.stop();
                        isover = true;
                        myc.repaint();
                    }
                    //更新小球坐标重汇界面
                    BX+=BXV;
                    BY+=BYV;
                    myc.repaint();
                }
            };
            timer = new Timer(100,task);
            timer.start();
            myc.setPreferredSize(new Dimension(W_Wdith,W_Height));
            s.add(myc);
            s.pack();
            s.setVisible(true);
            s.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }




    public static void main(String[] args) {
    GameingDemo G = new GameingDemo();
    G.mix();

    }}


